{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "<p align=\"center\">\n",
    "    <img src=\"https://github.com/GeostatsGuy/GeostatsPy/blob/master/TCG_color_logo.png?raw=true\" width=\"220\" height=\"240\" />\n",
    "\n",
    "</p>\n",
    "\n",
    "## Data Analytics \n",
    "\n",
    "### Interactive Parametric Distributions  in Python \n",
    "\n",
    "\n",
    "#### Michael Pyrcz, Associate Professor, The University of Texas at Austin \n",
    "\n",
    "##### [Twitter](https://twitter.com/geostatsguy) | [GitHub](https://github.com/GeostatsGuy) | [Website](http://michaelpyrcz.com) | [GoogleScholar](https://scholar.google.com/citations?user=QVZ20eQAAAAJ&hl=en&oi=ao) | [Book](https://www.amazon.com/Geostatistical-Reservoir-Modeling-Michael-Pyrcz/dp/0199731446) | [YouTube](https://www.youtube.com/channel/UCLqEr-xV-ceHdXXXrTId5ig)  | [LinkedIn](https://www.linkedin.com/in/michael-pyrcz-61a648a1)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Data Analytics: Parametric Distributions\n",
    "\n",
    "Here's a demonstration of making and general use of parametric distributions in Python. This demonstration is part of the resources that I include for my courses in Spatial / Subsurface Data Analytics at the Cockrell School of Engineering at the University of Texas at Austin.  \n",
    "\n",
    "#### Parametric Distributions\n",
    "\n",
    "I provide an set of interactives with thses parametric distributions:\n",
    "\n",
    "* Uniform\n",
    "* Triangular\n",
    "* Binomial\n",
    "* Poisson\n",
    "* Gaussian\n",
    "* Lognormal\n",
    "\n",
    "Change the distribution parameters and watch the probability density functions and cumulative distribution functions changes.\n",
    "\n",
    "I have a lecture on these parametric distributions available on [YouTube](https://www.youtube.com/watch?v=U7fGsqCLPHU&t=1687s).   \n",
    "\n",
    "#### Getting Started\n",
    "\n",
    "Here's the steps to get setup in Python with the GeostatsPy package:\n",
    "\n",
    "1. Install Anaconda 3 on your machine (https://www.anaconda.com/download/). \n",
    "2. From Anaconda Navigator (within Anaconda3 group), go to the environment tab, click on base (root) green arrow and open a terminal. \n",
    "3. In the terminal type: pip install geostatspy. \n",
    "4. Open Jupyter and in the top block get started by copy and pasting the code block below from this Jupyter Notebook to start using the geostatspy functionality. \n",
    "\n",
    "#### Importing Packages\n",
    "\n",
    "We will need some standard packages. These should have been installed with Anaconda 3."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np                        # ndarrys for gridded data\n",
    "import pandas as pd                       # DataFrames for tabular data\n",
    "import os                                 # set working directory, run executables\n",
    "import matplotlib.pyplot as plt           # for plotting\n",
    "from matplotlib.ticker import (MultipleLocator, AutoMinorLocator) # control of axes ticks\n",
    "plt.rc('axes', axisbelow=True)            # set axes and grids in the background for all plots\n",
    "from scipy import stats                   # summary statistics\n",
    "import math                               # trigonometry etc.\n",
    "import scipy.signal as signal             # kernel for moving window calculation\n",
    "import random                             # for randon numbers\n",
    "import seaborn as sns                     # for matrix scatter plots\n",
    "from scipy import linalg                  # for linear regression\n",
    "from sklearn import preprocessing\n",
    "import geostatspy.GSLIB as GSLIB\n",
    "from ipywidgets import interactive        # widgets and interactivity\n",
    "from ipywidgets import widgets                            \n",
    "from ipywidgets import Layout\n",
    "from ipywidgets import Label\n",
    "from ipywidgets import VBox, HBox"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Uniform Distribution\n",
    "\n",
    "We start with the uniform distribution.\n",
    "\n",
    "* a continuous, maximum uncertainty distribution, with constant density between the minimum and maximum values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7e342789bb1e4a308834c6bb92aaac7c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(Text(value='                                      Uniform Parametric Distribution Demonstration…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d27b7df9d1b64a85a02528ef55b7ebfc",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Output()"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# interactive calculation of the random sample set (control of source parametric distribution and number of samples)\n",
    "l = widgets.Text(value='                                      Uniform Parametric Distribution Demonstration, Michael Pyrcz, Associate Professor, The University of Texas at Austin',layout=Layout(width='950px', height='30px'))\n",
    "\n",
    "zmin = widgets.FloatSlider(min=0.0, max = 100.0, value = 10.0, description = 'Min',orientation='horizontal',layout=Layout(width='400px', height='50px'),continuous_update=False)\n",
    "zmin.style.handle_color = 'red'\n",
    "zmax = widgets.FloatSlider(min=0.0, max = 100.0, value = 30.0, description = 'Max',orientation='horizontal',layout=Layout(width='400px', height='50px'),continuous_update=False)\n",
    "zmax.style.handle_color = 'red'\n",
    "\n",
    "ui1 = widgets.HBox([zmin,zmax],kwargs = {'justify_content':'center'}) \n",
    "ui = widgets.VBox([l,ui1],kwargs = {'justify_content':'center'})\n",
    "\n",
    "def f_make(zmin,zmax):\n",
    "    xvals = np.linspace(0.0,100.0,1000)\n",
    "    pdf = stats.uniform.pdf(xvals, loc = zmin, scale = zmax-zmin)\n",
    "    cdf = stats.uniform.cdf(xvals, loc = zmin, scale = zmax-zmin)\n",
    "\n",
    "    plt.subplot(121)\n",
    "    plt.plot(xvals,pdf,color='black',alpha=0.8,linewidth=2)\n",
    "    plt.fill_between(xvals, 0, pdf, facecolor='darkorange', interpolate=True, alpha = 0.7)\n",
    "    plt.plot([0,100],[0,0],color='black')\n",
    "    plt.xlim(0,100); plt.gca().set_ylim(bottom=0.0)\n",
    "    plt.xlabel(\"$x$\"); plt.ylabel(\"Density\"); plt.title(\"Uniform Probability Density Function, $f_x(x)$\")\n",
    "    plt.grid(True, which='major',linewidth = 1.0); plt.grid(True, which='minor',linewidth = 0.2) # add y grids\n",
    "    plt.tick_params(which='major',length=7); plt.tick_params(which='minor', length=4)\n",
    "    plt.gca().xaxis.set_minor_locator(AutoMinorLocator()); plt.gca().yaxis.set_minor_locator(AutoMinorLocator()) # turn on minor ticks\n",
    " \n",
    "    plt.subplot(122)\n",
    "    plt.fill_between(xvals, 0, cdf, facecolor='darkorange', interpolate=True, alpha = 0.7)\n",
    "    plt.plot(xvals,cdf,color='black',alpha=0.8,linewidth=2)\n",
    "    plt.plot([0,100],[0,0],color='black')\n",
    "    plt.xlim(0,100); plt.ylim(0,1.0)\n",
    "    plt.xlabel(\"$x$\"); plt.ylabel(\"Cumulative Probability\"); plt.title(\"Uniform Cumulative Distribution Function, $F_x(x)$\")\n",
    "    plt.grid(True, which='major',linewidth = 1.0); plt.grid(True, which='minor',linewidth = 0.2) # add y grids\n",
    "    plt.tick_params(which='major',length=7); plt.tick_params(which='minor', length=4)\n",
    "    plt.gca().xaxis.set_minor_locator(AutoMinorLocator()); plt.gca().yaxis.set_minor_locator(AutoMinorLocator()) # turn on minor ticks\n",
    "    \n",
    "    plt.subplots_adjust(left=0.0, bottom=0.0, right=2.3, top=1.2, wspace=0.2, hspace=0.3)  \n",
    "    plt.plot()\n",
    "\n",
    "interactive_plot = widgets.interactive_output(f_make, {'zmin':zmin,'zmax':zmax})\n",
    "interactive_plot.clear_output(wait = True)                # reduce flickering by delaying plot updating    \n",
    "    \n",
    "display(ui, interactive_plot)                            # display the interactive plot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Binomial Distribution\n",
    "\n",
    "Now the binomial distribution.\n",
    "\n",
    "* a discrete distribution of the probablity of a specific number of successes given $P(success)$ and a number of trails, $n$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "bbea7da81caa412cb7fd579a497ed474",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(Text(value='                                      Binomial Parametric Distribution Demonstratio…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "66e74a27adde4d0faafdbefd82c894f4",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Output()"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# interactive calculation of the random sample set (control of source parametric distribution and number of samples)\n",
    "l = widgets.Text(value='                                      Binomial Parametric Distribution Demonstration, Michael Pyrcz, Associate Professor, The University of Texas at Austin',layout=Layout(width='950px', height='30px'))\n",
    "\n",
    "zp = widgets.FloatSlider(min=0.0, max = 1.0, value = 0.5, step = 0.01, description = '$P(Success)$',orientation='horizontal',layout=Layout(width='400px', height='50px'),continuous_update=False)\n",
    "zp.style.handle_color = 'red'\n",
    "zn = widgets.IntSlider(min=2, max = 100, value = 10, description = '$N_{trials}$',orientation='horizontal',layout=Layout(width='400px', height='50px'),continuous_update=False)\n",
    "zn.style.handle_color = 'red'\n",
    "\n",
    "ui1 = widgets.HBox([zp,zn],kwargs = {'justify_content':'center'}) \n",
    "ui = widgets.VBox([l,ui1],kwargs = {'justify_content':'center'})\n",
    "\n",
    "def f_make(zp,zn):\n",
    "    xvals = np.linspace(0,zn+1,zn+2)\n",
    "    pdf = stats.binom.pmf(xvals, zn, zp)\n",
    "    cdf = stats.binom.cdf(xvals, zn, zp)\n",
    "    \n",
    "    plt.subplot(121)\n",
    "    plt.plot(xvals,pdf,color='black',alpha=0.8,linewidth=2)\n",
    "    plt.fill_between(xvals, 0, pdf, facecolor='darkorange', interpolate=True, alpha = 0.7)\n",
    "    plt.plot([1,zn],[0,0],color='black')\n",
    "    plt.xlim(0,zn); plt.gca().set_ylim(bottom=0.0)\n",
    "    plt.xlabel(\"$x$\"); plt.ylabel(\"Density\"); plt.title(\"Binomial Probability Density Function, $f_x(x)$\")\n",
    "    plt.grid(True, which='major',linewidth = 1.0); plt.grid(True, which='minor',linewidth = 0.2) # add y grids\n",
    "    plt.tick_params(which='major',length=7); plt.tick_params(which='minor', length=4)\n",
    "    plt.gca().xaxis.set_minor_locator(AutoMinorLocator()); plt.gca().yaxis.set_minor_locator(AutoMinorLocator()) # turn on minor ticks    \n",
    "    \n",
    "    plt.subplot(122)\n",
    "    plt.fill_between(xvals, 0, cdf, facecolor='darkorange', interpolate=True, alpha = 0.7)\n",
    "    plt.plot(xvals,cdf,color='black',alpha=0.8,linewidth=2)\n",
    "    plt.plot([0,zn],[0,0],color='black')\n",
    "    plt.xlim(0,zn); plt.ylim(0,1.0)\n",
    "    plt.xlabel(\"$x$\"); plt.ylabel(\"Cumulative Probability\"); plt.title(\"Binomial Cumulative Distribution Function, $F_x(x)$\")\n",
    "    plt.grid(True, which='major',linewidth = 1.0); plt.grid(True, which='minor',linewidth = 0.2) # add y grids\n",
    "    plt.tick_params(which='major',length=7); plt.tick_params(which='minor', length=4)\n",
    "    plt.gca().xaxis.set_minor_locator(AutoMinorLocator()); plt.gca().yaxis.set_minor_locator(AutoMinorLocator()) # turn on minor ticks\n",
    "    \n",
    "    plt.subplots_adjust(left=0.0, bottom=0.0, right=2.3, top=1.2, wspace=0.2, hspace=0.3)  \n",
    "    plt.plot()\n",
    "\n",
    "interactive_plot = widgets.interactive_output(f_make, {'zp':zp,'zn':zn})\n",
    "interactive_plot.clear_output(wait = True)                # reduce flickering by delaying plot updating    \n",
    "    \n",
    "display(ui, interactive_plot)                            # display the interactive plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10., 11.])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "zn = 10\n",
    "np.linspace(0,zn+1,zn+2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Poisson Distribution\n",
    "\n",
    "Now the Poisson distribution.\n",
    "\n",
    "* a discrete distribution of the probablity of a specific number of successes given the average number of successes, $\\lambda$, over an interval."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "8a30aa7cf7464628b78955df967d8cd1",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(Text(value='                                      Poisson Parametric Distribution Demonstration…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c2e55248aaa8496db783398282a247f0",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Output()"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# interactive calculation of the random sample set (control of source parametric distribution and number of samples)\n",
    "l = widgets.Text(value='                                      Poisson Parametric Distribution Demonstration, Michael Pyrcz, Associate Professor, The University of Texas at Austin',layout=Layout(width='950px', height='30px'))\n",
    "\n",
    "zlambda = widgets.IntSlider(min=1, max = 15, value = 5, step = 1.0, description = '$\\\\lambda$',orientation='horizontal',layout=Layout(width='400px', height='50px'),continuous_update=False)\n",
    "zlambda.style.handle_color = 'red'\n",
    "\n",
    "ui1 = widgets.HBox([zlambda],kwargs = {'justify_content':'center'}) \n",
    "ui = widgets.VBox([l,ui1],kwargs = {'justify_content':'center'})\n",
    "\n",
    "def f_make(zlambda):\n",
    "    n = 30\n",
    "    xvals = np.linspace(0,n+1,n+2)\n",
    "    pdf = stats.poisson.pmf(xvals, zlambda)\n",
    "    cdf = stats.poisson.cdf(xvals, zlambda)\n",
    "    \n",
    "    plt.subplot(121)\n",
    "    plt.plot(xvals,pdf,color='black',alpha=0.8,linewidth=2)\n",
    "    plt.fill_between(xvals, 0, pdf, facecolor='darkorange', interpolate=True, alpha = 0.7)\n",
    "    plt.plot([0,n],[0,0],color='black')\n",
    "    plt.xlim(0,n); plt.ylim(0,0.40)\n",
    "    plt.xlabel(\"$x$\"); plt.ylabel(\"Density\"); plt.title(\"Poisson Probability Density Function, $f_x(x)$\")\n",
    "    plt.grid(True, which='major',linewidth = 1.0); plt.grid(True, which='minor',linewidth = 0.2) # add y grids\n",
    "    plt.tick_params(which='major',length=7); plt.tick_params(which='minor', length=4)\n",
    "    plt.gca().xaxis.set_minor_locator(AutoMinorLocator()); plt.gca().yaxis.set_minor_locator(AutoMinorLocator()) # turn on minor ticks\n",
    "     \n",
    "    plt.subplot(122)\n",
    "    plt.fill_between(xvals, 0, cdf, facecolor='darkorange', interpolate=True, alpha = 0.7)\n",
    "    plt.plot(xvals,cdf,color='black',alpha=0.8,linewidth=2)\n",
    "    plt.plot([0,n],[0,0],color='black')\n",
    "    plt.xlim(1,n); plt.ylim(0,1.0)\n",
    "    plt.xlabel(\"$x$\"); plt.ylabel(\"Cumulative Probability\"); plt.title(\"Poisson Cumulative Distribution Function, $F_x(x)$\")\n",
    "    plt.grid(True, which='major',linewidth = 1.0); plt.grid(True, which='minor',linewidth = 0.2) # add y grids\n",
    "    plt.tick_params(which='major',length=7); plt.tick_params(which='minor', length=4)\n",
    "    plt.gca().xaxis.set_minor_locator(AutoMinorLocator()); plt.gca().yaxis.set_minor_locator(AutoMinorLocator()) # turn on minor ticks\n",
    "        \n",
    "    plt.subplots_adjust(left=0.0, bottom=0.0, right=2.3, top=1.2, wspace=0.2, hspace=0.3)  \n",
    "    plt.plot()\n",
    "\n",
    "interactive_plot = widgets.interactive_output(f_make, {'zlambda':zlambda})\n",
    "interactive_plot.clear_output(wait = True)                # reduce flickering by delaying plot updating    \n",
    "    \n",
    "display(ui, interactive_plot)                            # display the interactive plot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Gaussian / Normal Distribution\n",
    "\n",
    "Now the Gaussian distribution.\n",
    "\n",
    "* an unbounded distribution (infinite tails) distribution parameterized by mean and standard deviation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "354f09004d6f4af186e561f52a97023f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(Text(value='                                      Gaussian Parametric Distribution Demonstratio…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f6e78c48379340b0aa97486516f4e2c9",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Output()"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# interactive calculation of the random sample set (control of source parametric distribution and number of samples)\n",
    "l = widgets.Text(value='                                      Gaussian Parametric Distribution Demonstration, Michael Pyrcz, Associate Professor, The University of Texas at Austin',layout=Layout(width='950px', height='30px'))\n",
    "\n",
    "zmean = widgets.FloatSlider(min=0.0, max = 100.0, value = 50.0, description = '$\\overline{x}$/$\\\\mu$',orientation='horizontal',layout=Layout(width='400px', height='50px'),continuous_update=False)\n",
    "zmean.style.handle_color = 'red'\n",
    "zstdev = widgets.FloatSlider(min=0.0, max = 30.0, value = 5.0, description = '$s$/$\\\\sigma$',orientation='horizontal',layout=Layout(width='400px', height='50px'),continuous_update=False)\n",
    "zstdev.style.handle_color = 'red'\n",
    "\n",
    "ui1 = widgets.HBox([zmean,zstdev],kwargs = {'justify_content':'center'}) \n",
    "ui = widgets.VBox([l,ui1],kwargs = {'justify_content':'center'})\n",
    "\n",
    "def f_make(zmean,zstdev):\n",
    "    xvals = np.linspace(0.0,100.0,1000)\n",
    "    pdf = stats.norm.pdf(xvals, loc = zmean, scale = zstdev)\n",
    "    cdf = stats.norm.cdf(xvals, loc = zmean, scale = zstdev)\n",
    "\n",
    "    plt.subplot(121)\n",
    "    plt.plot(xvals,pdf,color='black',alpha=0.8,linewidth=2)\n",
    "    plt.fill_between(xvals, 0, pdf, facecolor='darkorange', interpolate=True, alpha = 0.7)\n",
    "    plt.plot([0,100],[0,0],color='black')\n",
    "    plt.xlim(0,100); plt.gca().set_ylim(bottom=0.0)\n",
    "    plt.xlabel(\"$x$\"); plt.ylabel(\"Density\"); plt.title(\"Gaussian Probability Density Function, $f_x(x)$\")\n",
    "    plt.grid(True, which='major',linewidth = 1.0); plt.grid(True, which='minor',linewidth = 0.2) # add y grids\n",
    "    plt.tick_params(which='major',length=7); plt.tick_params(which='minor', length=4)\n",
    "    plt.gca().xaxis.set_minor_locator(AutoMinorLocator()); plt.gca().yaxis.set_minor_locator(AutoMinorLocator()) # turn on minor ticks\n",
    "    \n",
    "    plt.subplot(122)\n",
    "    plt.fill_between(xvals, 0, cdf, facecolor='darkorange', interpolate=True, alpha = 0.7)\n",
    "    plt.plot(xvals,cdf,color='black',alpha=0.8,linewidth=2)\n",
    "    plt.plot([0,100],[0,0],color='black')\n",
    "    plt.xlim(0,100); plt.ylim(0,1.0)\n",
    "    plt.xlabel(\"$x$\"); plt.ylabel(\"Density\"); plt.title(\"Gaussian Cumulative Distribution Function, $F_x(x)$\")\n",
    "    plt.grid(True, which='major',linewidth = 1.0); plt.grid(True, which='minor',linewidth = 0.2) # add y grids\n",
    "    plt.tick_params(which='major',length=7); plt.tick_params(which='minor', length=4)\n",
    "    plt.gca().xaxis.set_minor_locator(AutoMinorLocator()); plt.gca().yaxis.set_minor_locator(AutoMinorLocator()) # turn on minor ticks\n",
    "    \n",
    "    plt.subplots_adjust(left=0.0, bottom=0.0, right=2.3, top=1.2, wspace=0.2, hspace=0.3)  \n",
    "    plt.plot()\n",
    "\n",
    "interactive_plot = widgets.interactive_output(f_make, {'zmean':zmean,'zstdev':zstdev})\n",
    "interactive_plot.clear_output(wait = True)                # reduce flickering by delaying plot updating    \n",
    "    \n",
    "display(ui, interactive_plot)                            # display the interactive plot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Lognormal Distribution\n",
    "\n",
    "Now the lognormal distribution.\n",
    "\n",
    "* an unbounded distribution (infinite tails) distribution parameterized by mu and sigma."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "1d26e39611d2440597ecbe0b225b2fa5",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(Text(value='                                      LogNormal Parametric Distribution Demonstrati…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "26293cc8b86d491a9731e049e7081364",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Output()"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# interactive calculation of the random sample set (control of source parametric distribution and number of samples)\n",
    "l = widgets.Text(value='                                      LogNormal Parametric Distribution Demonstration, Michael Pyrcz, Associate Professor, The University of Texas at Austin',layout=Layout(width='950px', height='30px'))\n",
    "\n",
    "zmu = widgets.FloatSlider(min=0.1, max = 10.0, value = 1.0, description = 'Mu/$\\\\mu$',orientation='horizontal',layout=Layout(width='400px', height='50px'),continuous_update=False)\n",
    "zmu.style.handle_color = 'red'\n",
    "zsigma = widgets.FloatSlider(min=0.1, max = 3.0, value = 1.0, description = 'Sigma/$\\\\sigma$',orientation='horizontal',layout=Layout(width='400px', height='50px'),continuous_update=False)\n",
    "zsigma.style.handle_color = 'red'\n",
    "\n",
    "ui1 = widgets.HBox([zmu,zsigma],kwargs = {'justify_content':'center'}) \n",
    "ui = widgets.VBox([l,ui1],kwargs = {'justify_content':'center'})\n",
    "\n",
    "def f_make(zmu,zsigma):\n",
    "    xvals = np.linspace(0.0,100.0,1000)\n",
    "    pdf = stats.lognorm.pdf(xvals, s = zsigma, scale = math.exp(zmu))\n",
    "    cdf = stats.lognorm.cdf(xvals, s = zsigma, scale = math.exp(zmu))\n",
    "\n",
    "    plt.subplot(121)\n",
    "    plt.plot(xvals,pdf,color='black',alpha=0.8,linewidth=2)\n",
    "    plt.fill_between(xvals, 0, pdf, facecolor='darkorange', interpolate=True, alpha = 0.7)\n",
    "    plt.plot([0,100],[0,0],color='black')\n",
    "    plt.xlim(0,100); plt.gca().set_ylim(bottom=0.0)\n",
    "    plt.xlabel(\"$x$\"); plt.ylabel(\"Density\"); plt.title(\"Lognormal Probability Density Function, $f_x(x)$\")\n",
    "    plt.grid(True, which='major',linewidth = 1.0); plt.grid(True, which='minor',linewidth = 0.2) # add y grids\n",
    "    plt.tick_params(which='major',length=7); plt.tick_params(which='minor', length=4)\n",
    "    plt.gca().xaxis.set_minor_locator(AutoMinorLocator()); plt.gca().yaxis.set_minor_locator(AutoMinorLocator()) # turn on minor ticks\n",
    "      \n",
    "    plt.subplot(122)\n",
    "    plt.fill_between(xvals, 0, cdf, facecolor='darkorange', interpolate=True, alpha = 0.7)\n",
    "    plt.plot(xvals,cdf,color='black',alpha=0.8,linewidth=2)\n",
    "    plt.plot([0,100],[0,0],color='black')\n",
    "    plt.xlim(0,100); plt.ylim(0,1.0)\n",
    "    plt.xlabel(\"$x$\"); plt.ylabel(\"Density\"); plt.title(\"Lognormal Cumulative Distribution Function, $F_x(x)$\")\n",
    "    plt.grid(True, which='major',linewidth = 1.0); plt.grid(True, which='minor',linewidth = 0.2) # add y grids\n",
    "    plt.tick_params(which='major',length=7); plt.tick_params(which='minor', length=4)\n",
    "    plt.gca().xaxis.set_minor_locator(AutoMinorLocator()); plt.gca().yaxis.set_minor_locator(AutoMinorLocator()) # turn on minor ticks\n",
    "      \n",
    "    plt.subplots_adjust(left=0.0, bottom=0.0, right=2.3, top=1.2, wspace=0.2, hspace=0.3)  \n",
    "    plt.plot()\n",
    "\n",
    "interactive_plot = widgets.interactive_output(f_make, {'zmu':zmu,'zsigma':zsigma})\n",
    "interactive_plot.clear_output(wait = True)                # reduce flickering by delaying plot updating    \n",
    "    \n",
    "display(ui, interactive_plot)                            # display the interactive plot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are many other parametric distributions that we could have included. Also we could have demonstrated the distribution fitting. \n",
    "\n",
    "#### Comments\n",
    "\n",
    "This was a basic interactive demonstration of common parametric distributions. \n",
    "\n",
    "I have other demonstrations on the basics of working with DataFrames, ndarrays, univariate statistics, plotting data, declustering, data transformations, trend modeling and many other workflows available at [Python Demos](https://github.com/GeostatsGuy/PythonNumericalDemos) and a Python package for data analytics and geostatistics at [GeostatsPy](https://github.com/GeostatsGuy/GeostatsPy). \n",
    "  \n",
    "I hope this was helpful,\n",
    "\n",
    "*Michael*\n",
    "\n",
    "#### The Author:\n",
    "\n",
    "### Michael Pyrcz, Associate Professor, University of Texas at Austin \n",
    "*Novel Data Analytics, Geostatistics and Machine Learning Subsurface Solutions*\n",
    "\n",
    "With over 17 years of experience in subsurface consulting, research and development, Michael has returned to academia driven by his passion for teaching and enthusiasm for enhancing engineers' and geoscientists' impact in subsurface resource development. \n",
    "\n",
    "For more about Michael check out these links:\n",
    "\n",
    "#### [Twitter](https://twitter.com/geostatsguy) | [GitHub](https://github.com/GeostatsGuy) | [Website](http://michaelpyrcz.com) | [GoogleScholar](https://scholar.google.com/citations?user=QVZ20eQAAAAJ&hl=en&oi=ao) | [Book](https://www.amazon.com/Geostatistical-Reservoir-Modeling-Michael-Pyrcz/dp/0199731446) | [YouTube](https://www.youtube.com/channel/UCLqEr-xV-ceHdXXXrTId5ig)  | [LinkedIn](https://www.linkedin.com/in/michael-pyrcz-61a648a1)\n",
    "\n",
    "#### Want to Work Together?\n",
    "\n",
    "I hope this content is helpful to those that want to learn more about subsurface modeling, data analytics and machine learning. Students and working professionals are welcome to participate.\n",
    "\n",
    "* Want to invite me to visit your company for training, mentoring, project review, workflow design and / or consulting? I'd be happy to drop by and work with you! \n",
    "\n",
    "* Interested in partnering, supporting my graduate student research or my Subsurface Data Analytics and Machine Learning consortium (co-PIs including Profs. Foster, Torres-Verdin and van Oort)? My research combines data analytics, stochastic modeling and machine learning theory with practice to develop novel methods and workflows to add value. We are solving challenging subsurface problems!\n",
    "\n",
    "* I can be reached at mpyrcz@austin.utexas.edu.\n",
    "\n",
    "I'm always happy to discuss,\n",
    "\n",
    "*Michael*\n",
    "\n",
    "Michael Pyrcz, Ph.D., P.Eng. Associate Professor The Hildebrand Department of Petroleum and Geosystems Engineering, Bureau of Economic Geology, The Jackson School of Geosciences, The University of Texas at Austin\n",
    "\n",
    "#### More Resources Available at: [Twitter](https://twitter.com/geostatsguy) | [GitHub](https://github.com/GeostatsGuy) | [Website](http://michaelpyrcz.com) | [GoogleScholar](https://scholar.google.com/citations?user=QVZ20eQAAAAJ&hl=en&oi=ao) | [Book](https://www.amazon.com/Geostatistical-Reservoir-Modeling-Michael-Pyrcz/dp/0199731446) | [YouTube](https://www.youtube.com/channel/UCLqEr-xV-ceHdXXXrTId5ig)  | [LinkedIn](https://www.linkedin.com/in/michael-pyrcz-61a648a1)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
